<template>
	<view class="wrap">
		<!-- <view><yomol-upgrade :type="upgradeType" :url="upgradeUrl" title="发现新版本" :content="upgradeContent" ref="yomolUpgrade"></yomol-upgrade></view> -->
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎使用悦彩APP</view>
			<!-- <view><u-keyboard ref="uKeyboard" mode="number" v-model="show"></u-keyboard></view> -->
			<input class="u-border-bottom" type="number" v-model="tel" placeholder="手机号" />
			<view class="tips">未注册用户需绑定手机号</view>
			<input class="u-border-bottom" type="number" v-model="code" placeholder="验证码" />
			<view class="tips">短信验证码</view>
			<!-- <button @tap="submit" :style="[inputStyle]" class="getCaptcha">获取短信验证码</button> -->
			<u-verification-code :keep-running="true" :seconds="seconds" @end="end" @start="start" ref="uCode"
			@change="codeChange" :startText="startText" :changeText="changeText" 
			:endText="endText"></u-verification-code>
			<button @tap="getCode" :style="[inputStyle]" class="getCaptcha">{{tips}}</button>
			<button @tap="login" :style="[loginStyle]" class="login">完成绑定</button>
			<!-- <view class="alternative">
				<view @tap="goLogin" class="password">密码登录</view>
				<view class="issue">遇到问题</view>
			</view> -->
		</view>
		<view class="buttom">
			<!-- <view class="loginType">
				<view class="wechat item">
					<view class="icon"><u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon></view>
					微信
				</view>
				<view class="QQ item">
					<view class="icon"><u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon></view>
					QQ
				</view>
			</view> -->
			<view class="hint">
				登录代表同意
				<text class="link">用户协议、隐私政策，</text>
				并授权使用您的账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
		<view><u-toast ref="uToast" /></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			cache_key: '',
			encrypted_data: '',
			iv: '',

			tel: '', //手机号
			code: '', //验证码
			//
			tips: '',
			seconds: 60,
			refCode: null,
			startText: '获取验证码',
			changeText: 'X秒重新获取',
			endText: '重新获取'
		};
	},
	computed: {
		inputStyle() {
			let style = {};
			if (this.tel) {
				style.color = '#fff';
				style.backgroundColor = this.$u.color['warning'];
			}
			return style;
		},
		loginStyle() {
			let style = {};
			if (this.code) {
				style.color = '#fff';
				style.backgroundColor = this.$u.color['success'];
			}
			return style;
		}
	},
	onLoad() {
		//this.cache_key = uni.getStorageSync('cache_key');
		//console.log('cache_key=====');
		//console.log(this.cache_key);
		this.encrypted_data = uni.getStorageSync('encrypted_data');
		console.log('encrypted_data=====');
		console.log(this.encrypted_data);
		this.iv = uni.getStorageSync('iv');
		console.log('iv=====');
		console.log(this.iv);
	},
	methods: {
		//
		codeChange(text) {
			// console.log(text);
			this.tips = text;
		},
		getCode() {
			if (this.$u.test.mobile(this.tel)) {
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
						//发送短息验证码
						this.sendCode(this.tel);
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			}
			
		},
		end() {
			this.$u.toast('倒计时结束');
		},
		start() {
			this.$u.toast('倒计时开始');
		},

		//获取验证码按钮
		submit() {
			if (this.$u.test.mobile(this.tel)) {
				this.sendCode(this.tel);
			}
		},
		//校验验证码并完成认证
		login() {
			let _t = this;
			console.log('this.code====');
			console.log(_t.code);
			if (!_t.tel) {
				this.$refs.uToast.show({
					title: '请先输入手机号',
					type: 'error'
				});
				return;
			}
			const data = {
				phone: _t.tel,
				type: 2,
				code: _t.code
			};
			console.log('data=====');
			console.log(data);
			this.checkCode(data);
		},
		//发送验证码
		sendCode(tel) {
			let _t = this;
			console.log(tel);
			const data = {
				phone: _t.tel,
				type: 2
			};
			this.$u.api.sendCode(data).then(res => {
				if (res.data.status == 400) {
					this.$refs.uToast.show({
						title: res.data.message,
						type: 'default'
					});
				} else {
					this.$refs.uToast.show({
						title: '验证码发送成功',
						type: 'success'
					});
				}
				const data = res.data;
				console.log('sendCode');
				console.log(data);
			});
		},
		//校验验证码
		checkCode(data) {
			let _t = this;
			this.$u.api.checkCode(data).then(res => {
				console.log('checkCode=====');
				console.log(res);
				if (res.data == true) {
					this.$refs.uToast.show({
						title: '验证码正确',
						type: 'success'
					});
					//
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							const wxCode = loginRes.code;
							//获取用户oppenid
							_t.$u.api.xcxLogin({ code: wxCode }).then(res => {
								console.log('res');
								console.log(res);
								//新微信用户
								if (res.data.cache_key) {
									const binding_data = {
										cache_key: res.data.cache_key,
										encryptedData: _t.encrypted_data,
										iv: _t.iv,
										phone: _t.tel
									};
									_t.binding(binding_data);
								} else {
				
								}
							});
							//
						}
					});
				} else {
					this.$refs.uToast.show({
						title: '验证码错误',
						type: 'error'
					});
				}
			});
		},
		//绑定接口
		binding(data) {
			let _t = this;
			this.$u.api.binding(data).then(res => {
				console.log('binding=====');
				console.log(res);
				if (res.data.token) {
					_t.$refs.uToast.show({
						title: '绑定成功',
						type: 'success'
					});
					uni.setStorageSync('token', res.data.token);
					uni.setStorageSync('userInfo', res.data.user.user);
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else {
					/* this.$refs.uToast.show({
						title: '绑定失败，请重新操作',
						type: 'error'
					}); */
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.login {
			margin-top: 24rpx;
			background-color: rgb(113, 213, 161);
			color: #ffffff;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			&::after {
				border: none;
			}
		}
		.getCaptcha {
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;

			&::after {
				border: none;
			}
		}
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
	.buttom {
		.loginType {
			display: flex;
			padding: 350rpx 150rpx 150rpx 150rpx;
			justify-content: space-between;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: $u-content-color;
				font-size: 28rpx;
			}
		}

		.hint {
			padding: 40rpx 40rpx;
			font-size: 20rpx;
			color: $u-tips-color;

			.link {
				color: $u-type-warning;
			}
		}
	}
}
</style>
